﻿Imports System.Globalization

Public Class frmVentas

    Dim controller As New ControllerSigeco.controller_ventas
    Dim controllerStock As New ControllerSigeco.controller_stock

    Private Sub cbxVto_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxVto.CheckedChanged
        fechaVto.Enabled = cbxVto.Checked
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private _vistaVenta As Boolean = False

    Public Property vistaVenta() As Boolean
        Get
            Return _vistaVenta
        End Get
        Set(ByVal value As Boolean)
            _vistaVenta = value
        End Set
    End Property

    Private _CategoriaCliente As String = "precio1"

    Public Property categoriaCliente() As String
        Get
            Return _CategoriaCliente
        End Get
        Set(ByVal value As String)
            _CategoriaCliente = value
        End Set
    End Property

    Private _codRemision As Integer = 0

    Public Property codRemision() As Integer
        Get
            Return _codRemision
        End Get
        Set(ByVal value As Integer)
            _codRemision = value
        End Set
    End Property

    Private _codCliente As Integer = 0

    Public Property codCliente() As Integer
        Get
            Return _codCliente
        End Get
        Set(ByVal value As Integer)
            _codCliente = value
        End Set
    End Property

    Private _codMercaderia As Integer = 0
    Public Property codMercaderia() As Integer
        Get
            Return _codMercaderia
        End Get
        Set(ByVal value As Integer)
            _codMercaderia = value
        End Set
    End Property

    Private _codMercaderiaLote As Integer = 0
    Public Property codMercaderiaLote() As Integer
        Get
            Return _codMercaderiaLote
        End Get
        Set(ByVal value As Integer)
            _codMercaderiaLote = value
        End Set
    End Property

    Private _codMercaderiaDetalle As Integer = 0

    Public Property codMercaderiaDetalle() As Integer
        Get
            Return _codMercaderiaDetalle
        End Get
        Set(ByVal value As Integer)
            _codMercaderiaDetalle = value
        End Set
    End Property

    Private _codVenta As Integer = 0

    Public Property codVenta() As Integer
        Get
            Return _codVenta
        End Get
        Set(ByVal value As Integer)
            _codVenta = value
        End Set
    End Property

    Private _codNotaPedido As Integer = 0

    Public Property codNotaPedido() As Integer
        Get
            Return _codNotaPedido
        End Get
        Set(ByVal value As Integer)
            _codNotaPedido = value
        End Set
    End Property

    Private m_frmRegistroCobro As frmVentasCobro
    Public Property frmRegistroCobro() As frmVentasCobro
        Get
            Return m_frmRegistroCobro
        End Get
        Set(ByVal value As frmVentasCobro)
            m_frmRegistroCobro = value
        End Set
    End Property

    Private m_frmBusMercaderia As frmMercaderiaBuscador
    Public Property frmBusMercaderia() As frmMercaderiaBuscador
        Get
            Return m_frmBusMercaderia
        End Get
        Set(ByVal value As frmMercaderiaBuscador)
            m_frmBusMercaderia = value
        End Set
    End Property

    Private m_frmBusCliente As frmClienteBuscador
    Public Property frmBusCliente() As frmClienteBuscador
        Get
            Return m_frmBusCliente
        End Get
        Set(ByVal value As frmClienteBuscador)
            m_frmBusCliente = value
        End Set
    End Property

    Private Sub btnBuscProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscProveedor.Click

        frmBusCliente = New frmClienteBuscador
        frmBusCliente.ShowDialog()

        txtProveedor.Text = frmBusCliente.ClienteDesc
        codCliente = frmBusCliente.codCliente

        If frmBusCliente.ClienteCategoria <> "" Then
            categoriaCliente = frmBusCliente.ClienteCategoria
        End If


        If codCliente > 0 Then
            btnBuscMercaderia.Focus()
        End If

    End Sub

    Private Sub btnBuscOrdenCompra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscOrdenCompra.Click

    End Sub

    Private Sub btnBuscMercaderia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscMercaderia.Click

        If codCliente > 0 Then

            ErrorProvider1.SetError(txtProveedor, "")

            frmBusMercaderia = New frmMercaderiaBuscador
            frmBusMercaderia.esVenta = True
            If IsNumeric(txtMercaderia.Text) Then
                frmBusMercaderia.CodigoBarra = txtMercaderia.Text
            Else
                frmBusMercaderia.CodigoBarra = ""
            End If
            frmBusMercaderia.ShowDialog()

            txtMercaderia.Text = frmBusMercaderia.MercaderiaDesc
            codMercaderia = frmBusMercaderia.codMercaderia
            codMercaderiaLote = frmBusMercaderia.codMercaderiaLote

            If codMercaderia > 0 And codMercaderiaLote > 0 Then

                Dim dtPrecios As New DataTable
                dtPrecios = controllerStock.get_listas_precios_mercaderia(oiml.dbConn, codMercaderia, fechaFactura.Value)
                If Not dtPrecios Is Nothing Then
                    If dtPrecios.Rows.Count > 0 And categoriaCliente <> "" Then
                        txtprecio.Text = dtPrecios.Rows(0)(categoriaCliente)
                    Else
                        txtprecio.Text = 0
                        txttotal.Text = 0
                        MessageBox.Show("No posee precio registrado, favor verifique Lista de Precio", "Control de precios", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                Else
                    txtprecio.Text = 0
                    txttotal.Text = 0
                    MessageBox.Show("No posee precio registrado, favor verifique Lista de Precio", "Control de precios", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If

                txtcantidad.Focus()
                'cboIVA.Focus()
                'txtcantidad_Leave(sender, e)

            Else

                txtprecio.Text = 0
                txttotal.Text = 0
                'MessageBox.Show("No posee precio registrado, favor verifique Lista de Precio", "Control de precios", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End If

        Else
            ErrorProvider1.SetError(txtProveedor, "Debe seleccionar un cliente.")
        End If

    End Sub

    Private Sub txtcantidad_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcantidad.Leave
        If IsNumeric(txtcantidad.Text) And IsNumeric(txtprecio.Text) Then
            txttotal.Text = txtcantidad.Text * txtprecio.Text
        End If
    End Sub

    Private Sub frmVentas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvdetalle.AutoGenerateColumns = False
        btnImprimir.Visible = True
        cboTipo.SelectedIndex = 0
        cboEstado.SelectedIndex = 0

        oiml.CargarCombos(cboIVA, "Impuesto", "idImpuesto", controllerStock.get_impuestos(oiml.dbConn, 0, ""))
        oiml.CargarCombos(cboVendedor, "vendedor", "idvendedor", controller.get_Ventas_Vendedor(oiml.dbConn))

        CargarVentas()

    End Sub

    Private Sub CargarVentasDetalle()
        Try

            codMercaderia = 0
            codMercaderiaDetalle = 0
            txtMercaderia.Text = ""
            txtcantidad.Text = 1
            txtprecio.Text = 0
            txttotal.Text = 0

            Dim ds As New DataTable
            ds = controller.get_Ventas_Detalles_All(oiml.dbConn, codVenta)

            If Not ds Is Nothing Then
                dgvdetalle.DataSource = ds
            Else
                dgvdetalle.DataSource = Nothing
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Dim ci As CultureInfo

    Private Sub CargarVentas()
        Try
            If codVenta > 0 Then

                Dim ds As New DataTable
                ds = controller.get_Ventas_ByID(oiml.dbConn, codVenta)

                If Not ds Is Nothing Then

                    txtActualizadoFecha.Text = ds.Rows(0)("actualizadoFecha_")
                    txtActualizadoPor.Text = ds.Rows(0)("actualizadoPor")

                    If Not ds.Rows(0)("nroNotaPedido") Is DBNull.Value Then
                        codNotaPedido = ds.Rows(0)("idNotaPedido")
                        txtOrdenCompra.Text = ds.Rows(0)("nroNotaPedido")
                    End If

                    'ci = Thread.CurrentThread.CurrentCulture

                    codRemision = ds.Rows(0)("idNotaRemision")

                    If codRemision > 0 And Not vistaVenta Then
                        btnVerRemision.Visible = True
                    Else
                        btnVerRemision.Visible = False
                    End If

                    txtProveedor.Text = ds.Rows(0)("cliente")
                    codCliente = ds.Rows(0)("idCliente")
                    txtSaldo.Text = Decimal.Parse(ds.Rows(0)("totalFacturaSaldo").ToString).ToString("N0")

                    'ds.Rows(0)("totalFacturaSaldo").ToString("D")
                    txtTotalFactura.Text = Decimal.Parse(ds.Rows(0)("totalFactura").ToString).ToString("N0")
                    'ds.Rows(0)("totalFactura").ToString("D")
                    txtIVA.Text = Decimal.Parse(ds.Rows(0)("totalFacturaIVA").ToString).ToString("N0")
                    'ds.Rows(0)("totalFacturaIVA").ToString("D")
                    nroFactura.Text = ds.Rows(0)("nroComprobante")
                    fechaFactura.Value = ds.Rows(0)("fecha")

                    If Not ds.Rows(0)("fechaVencimiento") Is DBNull.Value Then
                        fechaVto.Value = ds.Rows(0)("fechaVencimiento")
                    End If

                    cboEstado.Text = ds.Rows(0)("estado")

                    ControlEstados()

                    cboTipo.Text = ds.Rows(0)("condicion")

                    'If cboTipo.Text = "CONTADO" And _
                    'ds.Rows(0)("estado") = "APROBADA" And _
                    'Decimal.Parse(ds.Rows(0)("saldo_cobro").ToString) > 0 Then
                    '    btnRegistrarCobro.Visible = True
                    'Else
                    '    btnRegistrarCobro.Visible = False
                    'End If

                    '*********************************************************************
                    'If cboTipo.Text = "CONTADO" And (ds.Rows(0)("estado") = "APROBADA" Or ds.Rows(0)("estado") = "ANULADA") Then
                    '    btnRegistrarCobro.Visible = True
                    'Else
                    '    btnRegistrarCobro.Visible = False
                    'End If

                    'btnGrabar.Enabled = True
                    'btnEliminar.Enabled = True

                    CargarVentasDetalle()

                End If

            Else
                cboEstado.Text = "EN PROCESO"
                nroFactura.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub ControlEstados()

        If cboEstado.Text = "APROBADA" Then

            nroFactura.Enabled = False
            fechaFactura.Enabled = False
            fechaVto.Enabled = False
            cboTipo.Enabled = False

            btnBuscMercaderia.Enabled = False
            btnBuscProveedor.Enabled = False
            btnBuscOrdenCompra.Enabled = False

            btnGrabar.Enabled = False
            btnAgregarDetalle.Enabled = False

            btnConfirmar.Visible = False

            btnEliminar.Enabled = False
            btnEliminarDetalle.Enabled = False
            cboVendedor.Enabled = False

            txtcantidad.Enabled = False
            txtprecio.Enabled = False
            cboIVA.Enabled = False

            btnCancelar.Focus()

        ElseIf cboEstado.Text = "ANULADA" Then

            nroFactura.Enabled = False
            fechaFactura.Enabled = False
            fechaVto.Enabled = False
            cboTipo.Enabled = False

            btnBuscMercaderia.Enabled = False
            btnBuscProveedor.Enabled = False
            btnBuscOrdenCompra.Enabled = False

            btnGrabar.Enabled = False
            btnAgregarDetalle.Enabled = False
            btnEliminarDetalle.Enabled = False
            btnConfirmar.Enabled = False
            btnEliminar.Enabled = False
            btnAnular.Enabled = False

            cboVendedor.Enabled = False

            txtcantidad.Enabled = False
            txtprecio.Enabled = False
            cboIVA.Enabled = False

            btnCancelar.Focus()

        Else
            btnConfirmar.Enabled = True
        End If

    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If nroFactura.Text = "" Then
            ErrorProvider1.SetError(nroFactura, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(nroFactura, "")
        End If        '

        If txtProveedor.Text = "" Then
            ErrorProvider1.SetError(txtProveedor, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtProveedor, "")
        End If

        If cboTipo.Text = "CREDITO" Then
            If (fechaFactura.Value > fechaVto.Value) Then
                ErrorProvider1.SetError(fechaVto, "La fecha de Vencimiento debe ser mayor a la fecha de registro")
                controlCampos = True
            Else
                ErrorProvider1.SetError(fechaVto, "")
            End If        '
        Else
            ErrorProvider1.SetError(fechaVto, "")
        End If

        Return controlCampos

    End Function

    Private Function ValidarCamposDetalle() As Boolean

        Dim controlCampos As Boolean = False

        If txtMercaderia.Text = "" Then
            ErrorProvider1.SetError(txtMercaderia, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtMercaderia, "")
        End If        '

        If txtcantidad.Text = "" Or Not IsNumeric(txtcantidad.Text) Then
            ErrorProvider1.SetError(txtcantidad, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtcantidad, "")
        End If        '

        If txtprecio.Text = "" Or Not IsNumeric(txtprecio.Text) Then
            ErrorProvider1.SetError(txtprecio, "Campo obligatorio")
            controlCampos = True
        Else

            If IsNumeric(txtprecio.Text) And txtprecio.Text = 0 Then
                MessageBox.Show("No posee precio registrado, favor verifique Lista de Precio", "Control de precios", MessageBoxButtons.OK, MessageBoxIcon.Information)
                ErrorProvider1.SetError(txtprecio, "Campo obligatorio")
                controlCampos = True
            Else
                ErrorProvider1.SetError(txtprecio, "")
            End If

        End If        '

        Return controlCampos

    End Function

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        If codVenta > 0 Then

            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Ventas", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                If controller.Delete_Ventas(oiml.dbConn, codVenta, oiml.idUsuario) Then
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & controller.MessageError.Message, "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        Dim fechaVencimiento As String = ""

        If cbxVto.Checked Then
            fechaVencimiento = fechaVto.Value
        End If

        If codVenta = 0 Then
            codVenta = controller.Insert_Ventas(oiml.dbConn, nroFactura.Text, fechaFactura.Value, _
                                                  codCliente, fechaVencimiento, cboTipo.Text, _
                                                  cboEstado.Text, codNotaPedido, 0, txtTotalFactura.Text, _
                                                  txtSaldo.Text, txtIVA.Text, oiml.idUsuario, Integer.Parse(cboVendedor.SelectedValue))

            If codVenta > 0 Then
                CargarVentas()
                MessageBox.Show("Registro grabado con exito!", "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else

            If controller.Update_Ventas(oiml.dbConn, codVenta, nroFactura.Text, fechaFactura.Value, _
                                                  codCliente, fechaVencimiento, cboTipo.Text, _
                                                  cboEstado.Text, codNotaPedido, 0, txtTotalFactura.Text, _
                                                  txtSaldo.Text, txtIVA.Text, oiml.idUsuario, Integer.Parse(cboVendedor.SelectedValue)) Then
                CargarVentas()
                MessageBox.Show("Registro grabado con exito!", "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)


            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub dgvdetalle_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvdetalle.KeyDown
        If e.KeyCode = Keys.Enter Then
            If dgvdetalle.SelectedRows.Count > 0 Then

                codMercaderiaDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

            End If
        End If
    End Sub

    Private Sub dgvdetalle_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvdetalle.DoubleClick
        If dgvdetalle.SelectedRows.Count > 0 Then

            codMercaderiaDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

        End If
    End Sub

    Private Sub dgvdetalle_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvdetalle.CellClick
        Try

            If dgvdetalle.SelectedRows.Count > 0 Then

                codMercaderiaDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnEliminarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarDetalle.Click

        If codMercaderiaDetalle > 0 Then

            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Ventas", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                If controller.Delete_Ventas_Detalle(oiml.dbConn, codMercaderiaDetalle, oiml.idUsuario, codVenta) Then
                    CargarVentas()

                Else
                    MessageBox.Show("Error: " & controller.MessageError.Message, "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Sub btnAgregarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarDetalle.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        If ValidarCamposDetalle() Then
            Exit Sub
        End If

        If codVenta = 0 Then
            codVenta = controller.Insert_Ventas(oiml.dbConn, nroFactura.Text, fechaFactura.Value, _
                                                  codCliente, fechaVto.Value, cboTipo.Text, _
                                                  cboEstado.Text, codNotaPedido, 0, txtTotalFactura.Text, _
                                                  txtSaldo.Text, txtIVA.Text, oiml.idUsuario, _
                                                  Integer.Parse(cboVendedor.SelectedValue))
        Else
            If controller.Update_Ventas(oiml.dbConn, codVenta, nroFactura.Text, fechaFactura.Value, _
                                                  codCliente, fechaVto.Value, cboTipo.Text, _
                                                  cboEstado.Text, codNotaPedido, 0, txtTotalFactura.Text, _
                                                  txtSaldo.Text, txtIVA.Text, oiml.idUsuario, Integer.Parse(cboVendedor.SelectedValue)) Then
            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

        If codVenta > 0 And codMercaderia > 0 And codMercaderiaLote > 0 Then

            If controller.Insert_Ventas_Detalle(oiml.dbConn, codVenta, codMercaderia, _
                                                 txtcantidad.Text, txtprecio.Text, _
                                                 cboIVA.SelectedValue, txttotal.Text, _
                                                 oiml.idUsuario, codMercaderiaLote, "", _
                                                 0, False) Then
                CargarVentas()
                MessageBox.Show("Registro grabado con exito!", "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If

    End Sub

    Private Sub btnConfirmar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmar.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        If codVenta > 0 Then

            If controller.Update_Ventas_Estado(oiml.dbConn, codVenta, "APROBADA", oiml.idUsuario, nroFactura.Text, oiml.idMotivoVenta) Then
                CargarVentas()
                MessageBox.Show("Registro confirmado con exito!", "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If

    End Sub

    Private Sub cboTipo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTipo.SelectedIndexChanged

        If cboTipo.Text = "CREDITO" Then
            cbxVto.Checked = True
        Else
            cbxVto.Checked = False
        End If

    End Sub

    Private Sub txtprecio_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtprecio.Leave
        If IsNumeric(txtcantidad.Text) And IsNumeric(txtprecio.Text) Then
            txttotal.Text = txtcantidad.Text * txtprecio.Text
        End If
    End Sub

    Private Sub btnAnular_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnular.Click
        If codVenta > 0 Then
            If MessageBox.Show("Desea anular el registro seleccionado?", "Ventas", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                If controller.Update_Ventas_Estado(oiml.dbConn, codVenta, "ANULADA", oiml.idUsuario, nroFactura.Text, 0) Then
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & controller.MessageError.Message, "Ventas", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If
        End If
    End Sub

    Private Sub txtMercaderia_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnBuscMercaderia_Click(sender, e)
        End If
    End Sub

    

    Private Sub txtMercaderia_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMercaderia.Enter
        txtMercaderia.Text = ""
        codMercaderia = 0
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerRemision.Click
        If codRemision > 0 Then
            Dim compra As New frmNotaRemision
            compra.vistaVenta = True
            compra.codNotaRemision = codRemision
            compra.ShowDialog()
        End If

    End Sub

    Private m_frmReporte As frmReportes
    Public Property frmRpts() As frmReportes
        Get
            Return m_frmReporte
        End Get
        Set(ByVal value As frmReportes)
            m_frmReporte = value
        End Set
    End Property

    Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click

        If codVenta > 0 Then
            Dim dtFactura As New DataTable
            dtFactura = controller.get_Ventas_Detalles_Imprimir(oiml.dbConn, codVenta)

            Dim conv As New Conversiones.Conv()


            If frmRpts Is Nothing Then
                frmRpts = New frmReportes
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "FACTURA"
                frmRpts.datos_Reporte = dtFactura
                frmRpts.MontoLetras = "GUARANIES " & conv.enletras(txtTotalFactura.Text, False)
                frmRpts.ShowDialog()
            Else

                If Not frmRpts.Visible Then
                    frmRpts = New frmReportes()
                    'frmRpts.MdiParent = Me
                    frmRpts.tipo_Reporte = "FACTURA"
                    frmRpts.datos_Reporte = dtFactura
                    frmRpts.MontoLetras = "GUARANIES " & conv.enletras(txtTotalFactura.Text, False)
                    frmRpts.ShowDialog()
                Else
                    frmRpts.tipo_Reporte = "FACTURA"
                    frmRpts.datos_Reporte = dtFactura
                    frmRpts.MontoLetras = "GUARANIES " & conv.enletras(txtTotalFactura.Text, False)
                    frmRpts.Select()
                    frmRpts.Activate()
                End If

            End If

        End If

    End Sub

    Private Sub btnRegistrarCobro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegistrarCobro.Click

        If codVenta > 0 And codCliente > 0 And cboEstado.Text = "APROBADA" And cboTipo.Text = "CONTADO" Then

            frmRegistroCobro = New frmVentasCobro
            frmRegistroCobro.codVenta = codVenta
            frmRegistroCobro.codCliente = codCliente
            frmRegistroCobro.ShowDialog()
            CargarVentas()

        End If

    End Sub

End Class